package com.tech.koufu.ui.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.lidroid.xutils.BitmapUtils;
import com.tech.koufu.R;
import com.tech.koufu.action.RequestQueueSingleton;
import com.tech.koufu.model.TrackerUser;
import com.tech.koufu.tools.CookieStringRequest;
import com.tech.koufu.tools.Statics; 
import com.tech.koufu.ui.activity.menu.AttentionFansFragment;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AttenFansAdapter extends BaseAdapter {
	private Context context;  
	private LayoutInflater mInflater = null; 
	private int type = 1;
	private BitmapUtils bitmapUtils;
	public ArrayList<TrackerUser> datas;
	private CookieStringRequest stringRequest;
	public String sessionID;
	public String userId;
	
	public AttenFansAdapter(Context context,int type,BitmapUtils bitmapUtils) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.type = type;
		this.bitmapUtils = bitmapUtils;
	}
	@Override
	public int getCount() {
		if(datas!=null){
			return datas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) { 
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_fans, parent,false); 
             holder.icon = (ImageView)convertView.findViewById(R.id.iv_head);
             holder.txt_experts_name = (TextView)convertView.findViewById(R.id.txt_experts_name);  
             holder.txt_experts_profit = (TextView)convertView.findViewById(R.id.txt_experts_profit);   
             holder.btn_track = (Button)convertView.findViewById(R.id.btn_track);
             
             //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag  
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         final TrackerUser tu = datas.get(position);
         holder.txt_experts_name.setText(tu.trackName);
          
    	  SpannableStringBuilder ssb =  new SpannableStringBuilder(String.format(context.getResources().getString(R.string.txt_profitrate_s), tu.zongup));
          ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.bgBlackColor_63)),0,4,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
          holder.txt_experts_profit.setText(ssb);
    
         
         if(tu.avator!=null){
        	 bitmapUtils.display(holder.icon , tu.avator);
         }
        if(type==1){
        	final int _pos = position;
        	holder.btn_track.setText(R.string.btn_cancle_concern);
        	holder.btn_track.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) { 
					removeConcern(tu,_pos);
				}
			});
        }else{
        	holder.btn_track.setVisibility(View.GONE);
        	
        }
         return convertView;
	}
	private void removeConcern(final TrackerUser tu,final int position){
		String url = Statics.URL_PHP+Statics.IF_removeConcern; 
		stringRequest = new CookieStringRequest(Method.POST, url, new Listener<String>() {

			@Override
			public void onResponse(String response) {   
				if(!TextUtils.isEmpty(response)){
					try {  
//						Log.w("Tt", response);
						JSONObject obj = new JSONObject(response);
						String status = obj.optString("status");
						String data = obj.optString("data");
						Toast.makeText(context, data, Toast.LENGTH_SHORT).show();
						if(status.equals("0")){
							datas.remove(position);
							AttenFansAdapter.this.notifyDataSetChanged();
						}
					} catch (JSONException e) { 
						e.printStackTrace();
					}
				} 
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) { 
				
			}
		}){
			@Override
			protected Map<String, String> getParams() throws AuthFailureError { 
				Map<String, String> params = new HashMap<String, String>(); 
				params.put("concern_user_id", tu.trackId);  
				params.put("userID", userId);
//				Log.w("Tt", params.toString());
				return params;
			}
		}; 
		stringRequest.setHeader(sessionID);
		stringRequest.setTag(AttentionFansFragment.TAG);
		RequestQueueSingleton.getInstance(context).addToRequestQueue(stringRequest);
	}
	 
	static class ViewHolder  
    {   
		public ImageView icon;
        public TextView txt_experts_name;  
        public TextView txt_experts_profit;   
        public Button btn_track;
        
    }  
	 

}
